package com.admin.dbdao;

import com.admin.bean.ApplyRefuse;
import com.data.bean.RegistMassage;
import com.data.dbdao.BaseDao;
import com.data.util.FinalStringUtils;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 开发者管理
 *
 * @author:HingLo
 * @create 2017-09-13 15:12
 **/
@Repository
@Transactional
public class DeveloperApplyDao extends BaseDao {
    /**
     * 查询公司账号或者个人开发者信息
     *
     * @param type
     * @return
     */
    public List<?> findAllDeveloperApplyDao(int type) {
        String hql = "";
        if (type == FinalStringUtils.PERSON) {
            hql = "select RegistMassage.id as id,RegistMassage.name as name ,RegistMassage.number as number,PersonMassage.ctime as ctime,PersonMassage.email as email,RegistMassage.state as state from RegistMassage as RegistMassage,PersonMassage as PersonMassage  where PersonMassage.id=RegistMassage.id and RegistMassage.state=? and PersonMassage.estate=?";
        } else if (type == FinalStringUtils.COMPANY) {
            hql = "select RegistMassage.id as id,RegistMassage.name as name ,RegistMassage.number as number,CompanyMassage.ctime as ctime,CompanyMassage.email as email,RegistMassage.state as state from RegistMassage as RegistMassage,CompanyMassage as CompanyMassage  where RegistMassage.id=CompanyMassage.id and RegistMassage.state=? and CompanyMassage.estate=?";
        } else return null;
        return this.findMassageDao(hql, FinalStringUtils.DEFAULT, true);
    }

    public RegistMassage lookIconDao(String id) {
        String hql = "from RegistMassage where id=?";
        List<?> list = findMassageDao(hql, id);
        return list.size() > 0 ? (RegistMassage) list.get(0) : null;
    }

    /**
     * 通过账号申请
     *
     * @param type
     * @param id
     * @return
     */
    public boolean updateApplyDao(int type, String id) {
        String sql = "update RegistMassage as r , Account as a set a.istate=? ,r.state=? where r.id=a.id and r.id=?";
        return super.updateMysqlMethodDao(sql, true, FinalStringUtils.ACTIVE, id) > 0;
    }

    public boolean updateApplyDao(String id) {
        String hql = "update RegistMassage as r  set r.state=? where r.id=?";
        return super.updateOneMssageDao(hql, FinalStringUtils.REFUSE, id);
    }

    /**
     * 添加到未通过的原因表中
     *
     * @param applyRefuse
     * @return
     */
    public boolean refuseApplyDao(ApplyRefuse applyRefuse) {
        return saveMassageDao(applyRefuse);
    }

    /**
     * 拒绝列表
     *
     * @return
     */
    public List<?> applyRefuseDao() {
        String hql = "select a.id,a.uid,a.content,a.ctime,account.phone,account.email,r.state from Account as account, ApplyRefuse as a ,RegistMassage as r where r.id=a.uid and r.id=account.id";
        return super.findMassageDao(hql);
    }

    /**
     * 删除未通过列表中的信息
     *
     * @param id
     * @return
     */
    public boolean deleteApplyDao(String id) {
        String hql = "delete ApplyRefuse where id=?";
        return super.updateManyMssageDao(hql, id) > 0;
    }

}
